# -*- coding: utf-8 -*-
import sys
import io
import json
import requests

if sys.platform == 'win32':
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

target_version = "26.0.1"
device_model = "iPhone16,2"

print(f"搜索版本: {target_version}")
print(f"设备型号: {device_model}")
print("=" * 80)

all_ios_url = "https://api.appledb.dev/ios/main.json"

try:
    response = requests.get(all_ios_url, timeout=15)
    if response.status_code == 200:
        all_versions = response.json()
        print(f"[OK] 获取到 {len(all_versions)} 个iOS/iPadOS版本")
        
        # 搜索所有包含26.0.1的版本
        matching_versions = []
        for ios in all_versions:
            version = str(ios.get('version', ''))
            if '26.0.1' in version:
                matching_versions.append(ios)
        
        print(f"\n找到 {len(matching_versions)} 个包含 '26.0.1' 的版本:")
        print("=" * 80)
        
        for ios in matching_versions:
            version = ios.get('version', 'N/A')
            build = ios.get('build', 'N/A')
            os_type = ios.get('osStr', 'iOS')
            unique_build = ios.get('uniqueBuild', build)
            
            print(f"\n版本: {version}")
            print(f"  系统: {os_type}")
            print(f"  Build: {build}")
            print(f"  唯一标识: {unique_build}")
            print(f"  发布日期: {ios.get('released', 'N/A')}")
            
            # 检查这个版本的URL
            # 尝试获取详细信息
            detail_url = f"https://api.appledb.dev/ios/{unique_build}.json"
            try:
                detail_resp = requests.get(detail_url, timeout=10)
                if detail_resp.status_code == 200:
                    detail_data = detail_resp.json()
                    
                    if 'deviceMap' in detail_data:
                        device_map = detail_data['deviceMap']
                        
                        # 检查我们的设备
                        if device_model in device_map:
                            device_info = device_map[device_model]
                            signed = device_info.get('signed', False)
                            print(f"  [{device_model}] 签名状态: {'✓ 已开放' if signed else '✗ 已关闭'}")
                        else:
                            print(f"  [{device_model}] 不支持此设备")
                            print(f"  支持设备数: {len(device_map)}")
                            # 显示前5个支持的设备
                            supported_devices = list(device_map.keys())[:5]
                            print(f"  示例设备: {', '.join(supported_devices)}")
                    else:
                        print(f"  [警告] 无设备兼容性信息")
                else:
                    print(f"  [无法获取详细信息] HTTP {detail_resp.status_code}")
            except Exception as e:
                print(f"  [错误] 获取详细信息失败: {e}")
        
        # 如果没找到，显示最接近的版本
        if not matching_versions:
            print(f"\n未找到包含 '{target_version}' 的版本")
            print(f"\n显示版本号以'26'开头的版本:")
            for ios in all_versions[:200]:
                version = str(ios.get('version', ''))
                if version.startswith('26'):
                    os_type = ios.get('osStr', 'iOS')
                    build = ios.get('build', 'N/A')
                    print(f"  - {version} ({os_type}) Build: {build}")
    
except Exception as e:
    print(f"[错误] {e}")
    import traceback
    traceback.print_exc()

